package com.example.teaching_server.dto;

import com.example.teaching_server.entity.ActivityCheckinEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 打卡结果数据传输对象
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "活动打卡记录信息")
public class CheckinResultDTO {

    @NotNull(message = "打卡记录ID不能为空")
    @Schema(description = "打卡记录唯一标识", example = "1")
    private Long id;

    @NotNull(message = "活动ID不能为空")
    @Schema(description = "关联活动ID", example = "1")
    private Long activityId;

    @NotBlank(message = "活动标题不能为空")
    @Size(max = 200, message = "活动标题长度不能超过200个字符")
    @Schema(description = "活动标题", example = "2024年春季科技创新营")
    private String activityTitle;

    @NotNull(message = "用户ID不能为空")
    @Schema(description = "打卡用户ID", example = "1001")
    private Long userId;

    @NotNull(message = "打卡日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "打卡日期", example = "2024-03-01")
    private LocalDate checkDate;

    @NotNull(message = "打卡时间不能为空")
    @JsonFormat(pattern = "HH:mm:ss")
    @Schema(description = "打卡时间", example = "09:00:00")
    private LocalTime checkTime;

    @Size(max = 500, message = "打卡说明长度不能超过500个字符")
    @Schema(description = "打卡说明或备注", example = "按时到达活动现场")
    private String description;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "记录创建时间", example = "2024-03-01 09:05:00")
    private LocalDateTime createdAt;

    /**
     * 从实体类转换为DTO
     *
     * @param entity 打卡记录实体
     * @return 打卡记录DTO
     */
    public static CheckinResultDTO fromEntity(ActivityCheckinEntity entity) {
        if (entity == null) {
            return null;
        }

        return CheckinResultDTO.builder()
                .id(entity.getId())
                .activityId(entity.getActivity() != null ? entity.getActivity().getId() : null)
                .activityTitle(entity.getActivity() != null ? entity.getActivity().getTitle() : null)
                .userId(entity.getUserId())
                .checkDate(entity.getCheckDate())
                .checkTime(entity.getCheckTime())
                .description(entity.getDescription())
                .createdAt(entity.getCreatedAt())
                .build();
    }

    /**
     * 获取打卡状态描述
     *
     * @return 打卡状态
     */
    @Schema(description = "打卡状态", accessMode = Schema.AccessMode.READ_ONLY)
    public String getCheckinStatus() {
        if (checkTime == null) {
            return "未打卡";
        }
        return "已打卡";
    }
}
